package com.clippick.dbbusiness;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

import com.clippick.database.ClipData;
import com.clippick.database.FeedOrderDBAdapter;
import com.clippick.database.HistoryDBAdapter;

public class DBBusiness {
	private static HistoryDBAdapter dbHistory;
	private static FeedOrderDBAdapter dbFeedOrder;
	
	public static void startDBs(Context c,
						String basicTableName){
		dbHistory=new HistoryDBAdapter(c,basicTableName);
		dbFeedOrder=new FeedOrderDBAdapter(c,basicTableName);
	}
	
	public static void openHistoryDb(){
		dbHistory.open();
	}
	
	public static void closeHistoryDb(){
		dbHistory.close();
	}
	
	public static void openFeedOrderDb(){
		dbFeedOrder.open();
	}
	
	public static void closeFeedOrderDb(){
		dbFeedOrder.close();
	}
	
	public static void openDbs(){
		dbHistory.open();
		dbFeedOrder.open();
	}
	
	public static void closeDbs(){
		dbHistory.close();
		dbFeedOrder.close();
	}
	
	@SuppressWarnings("rawtypes")
	public static List<Map> fetchStarClipFromDb(){
		dbHistory.open();
		dbFeedOrder.open();
		List<Map> retList=ClipData.fetchStarClipFromDb(
							dbHistory,dbFeedOrder);
		
		dbHistory.close();
		dbFeedOrder.close();
		return retList;
	}
	
	@SuppressWarnings("rawtypes")
	public static List<Map> fetchClipFromDb(){
		dbHistory.open();
		dbFeedOrder.open();

		List<Map> retList=ClipData.fetchClipFromDb(
						dbHistory,dbFeedOrder);
		
		dbHistory.close();
		dbFeedOrder.close();
		
		return retList;
	}
	
	/**
	 * warning: this function doesn't open/close database. 
	 * you have to manage the database yourself using 
	 * <b>open()</b> and <b>close()</b> function 
	 * corresponding
	 * 
	 * @param index
	 * @return
	 */
	public static Map<Object, Object> compareFeed(){
		//dbHistory.open();
		Map<Object,Object> retMap=ClipData.
						compareFeed(dbHistory);
		//dbHistory.close();
		return retMap;
	}

	/**
	 * warning: this function doesn't open/close database. 
	 * you have to manage the database yourself using 
	 * <b>open()</b> and <b>close()</b> function 
	 * corresponding
	 * 
	 * @param index
	 * @return
	 */
	public static long insertFeedFromFeedOrder(long idx, 
			String order, String textOrPath,String imageUrl) {
		//dbFeedOrder.open();
		long retVal=dbFeedOrder.insertFeed(idx, order, 
							textOrPath, imageUrl);
		//dbFeedOrder.close();
		return retVal;
	}
	
	/**
	 * warning: this function returns a Cursor object and
	 * doesn't open/close database. you have to manage the
	 * database yourself using <b>open()</b> and <b>close()</b>
	 * function corresponding
	 * 
	 * @param idx
	 * @return
	 * @throws SQLException
	 */
	public static Cursor fetchFeedByIndex(long idx) 
								throws SQLException {
		return dbFeedOrder.fetchFeedByIndex(idx);
	}
	
	/**
	 * warning: this function doesn't open/close database. 
	 * you have to manage the database yourself using 
	 * <b>open()</b> and <b>close()</b> function 
	 * corresponding
	 * 
	 * @param index
	 * @return
	 */
	public static boolean deleteFeedFromFeedOrder(long index) {
		return dbFeedOrder.deleteFeedByIndex(index);
	}
	
	/**
	 * warning: this function doesn't open/close database. 
	 * you have to manage the database yourself using 
	 * <b>open()</b> and <b>close()</b> function 
	 * corresponding
	 * 
	 * @param index
	 * @return
	 */
	public static long insertFeedFromHistory(
						Map<String, String> feed) {
		//dbHistory.open();
		long retVal=dbHistory.insertFeed(feed);
		//dbHistory.close();
		return retVal;
	}

	/**
	 * warning: this function doesn't open/close database. 
	 * you have to manage the database yourself using 
	 * <b>open()</b> and <b>close()</b> function 
	 * corresponding
	 * 
	 * @param feed
	 * @return
	 */
	public static boolean updateFeedFromHistoryDb(
							Map<String, String> feed) {
		return dbHistory.updateFeed(feed);
	}

	/**
	 * warning: this function doesn't open/close database. 
	 * you have to manage the database yourself using 
	 * <b>open()</b> and <b>close()</b> function 
	 * corresponding
	 * 
	 * @param feed
	 * @return
	 */
	public static long insertFeedIntoTempTable(
						Map<String, String> feed) {
		return dbHistory.insertFeedIntoTempTable(feed);
	}
	
	public static List<String> getAllText() {
		openHistoryDb();
		List<String> retList=dbHistory.getAllText();
		closeHistoryDb();
		return retList;
	}
	
	/**
	 * warning: this function returns a Cursor object and
	 * doesn't open/close database. you have to manage the
	 * database yourself using <b>open()</b> and <b>close()</b>
	 * function corresponding
	 * 
	 * @param index
	 * @return Cursor
	 * @throws SQLException
	 */
	public static Cursor getFeedByIndex(long index) 
						throws SQLException {
		return dbHistory.getFeedByIndex(index);
	}
	
	/**
	 * warning: this function doesn't open/close database. 
	 * you have to manage the database yourself using 
	 * <b>open()</b> and <b>close()</b> function 
	 * corresponding
	 * 
	 * @param index
	 * @return
	 */
	public static boolean deleteFeedFromHistoryDb(
							long index) {
		return dbHistory.deleteFeed(index);
	}
}
